/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.whu.weibo_souhu.login;

import org.apache.http.client.HttpClient;
import javax.net.ssl.SSLContext;
//import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
//import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
//import java.security.cert.X509Certificate;
import java.security.cert.CertificateException;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author Administrator
 */
public class WebClientDevWrapper {

    static HttpClient wrapClient(HttpClient httpclient) {
        try {
                        SSLContext ctx = SSLContext.getInstance("TLS");
                        X509TrustManager tm = new X509TrustManager() {
                            public void checkClientTrusted(java.security.cert.X509Certificate[] xcs, String string) throws CertificateException {throw new UnsupportedOperationException("Not supported yet.");}
                            public void checkServerTrusted(java.security.cert.X509Certificate[] xcs, String string) throws CertificateException {throw new UnsupportedOperationException("Not supported yet.");}
                            public java.security.cert.X509Certificate[] getAcceptedIssuers() {throw new UnsupportedOperationException("Not supported yet.");}
                        };                 
                        ctx.init(null, new TrustManager[]{tm}, null);
                        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                        //ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                        
                        ClientConnectionManager ccm = httpclient.getConnectionManager();
                        SchemeRegistry sr = ccm.getSchemeRegistry();
      
                        sr.register(new Scheme("https",443,ssf));
                        return new DefaultHttpClient(ccm, httpclient.getParams());
                } catch (Exception ex) {
                        ex.printStackTrace();
                        return null;
                }
    }
    
}
